<?php
function create_admin_menu() {
	add_menu_page('Woodle', 'Woodle', 5, 'woodle_admin', 'menu_woodle_main');
	add_submenu_page('woodle_admin', 'Courses', 'Courses', 5, "woodle_admin",'menu_woodle_main');
}

function addAdminToHeader() {
	echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/woodle/css/jquery.dataTables_themeroller.css" />' . "\n";
	echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/woodle/css/jquery-ui-1.8.20.custom.css" />' . "\n";
	wp_enqueue_script('dataTable', get_bloginfo('wpurl') . '/wp-content/plugins/woodle/js/jquery.dataTables.js', array('jquery'));
	wp_enqueue_script('timePicker', get_bloginfo('wpurl') . '/wp-content/plugins/woodle/js/jquery-ui-timepicker-addon.js', array('jquery', 'jquery-ui-slider'));
	wp_enqueue_script('adminScript', get_bloginfo('wpurl') . '/wp-content/plugins/woodle/js/woo_admin_script.js', array('jquery', 'jquery-ui-dialog', 'jquery-ui-datepicker'));
}

function menu_woodle_main() {
	echo '<div style="display:none" id="confirm_deletion" title="Confirmation Required"></div>';
	if (isset($_GET["action"])) {
		load_page($_GET["action"]);
	} else {
		include_once "woodle_course_list.php"; //default
	}
}

function load_page($action) {
	global $woodb;
	switch ($action) {
		case 'woodle_add_course':
			include "woodle_add_course.php";
			break;
		case 'woodle_lecture_list':
			include "woodle_lecture_list.php";
			break;
			
		case 'woodle_add_lecture':
			include "woodle_add_lecture.php";
			break;
		
		case 'woodle_edit_lecture':
			include "woodle_edit_lecture.php";
			include "woodle_material_list.php";
			break;
			
		case 'woodle_edit_course':
			include "woodle_edit_course.php";
			include "woodle_lecture_list.php";
			break;
		
		case 'woodle_import_material':
			include "woodle_import_material.php";
			break;
			
		case 'woodle_remove_lecture':
			$lecture_id = isset($_POST["lecture_id"]) ? $_POST["lecture_id"] : null;
			if ($lecture_id != null) {
				$woodb->deleteLecture($lecture_id);
			}
			break;
			
		case 'woodle_remove_material':
			$material_id = isset($_POST["material_id"]) ? $_POST["material_id"] : null;
			if ($material_id != null) {
				$woodb->deleteMaterial($material_id);
			}
			break;
		
		case 'woodle_edit_course':
			include "woodle_edit_course.php";
			break;
			
		case 'woodle_remove_course':
			$course_id = isset($_POST["course_id"]) ? $_POST["course_id"] : null;
			if ($course_id != null) {
				$woodb->deleteCourse($course_id);
			}
			break;
		
		case 'woodle_duplicate_course':
			if (isset($_REQUEST['course_id']) && $_REQUEST['course_id'] != NULL) {
				$new_course_id = $woodb->duplicateCourse($_REQUEST['course_id']);
				
				if ($new_course_id != NULL) {
					$_SERVER['REQUEST_METHOD'] = 'GET';
					$_GET['course_id'] = $new_course_id;
					load_page('woodle_edit_course');
				}
			}	
		
		case 'woodle_upload_file':
			if ( isset( $_POST['html-upload'] ) && isset( $_POST['lecture_id'] ) && !empty( $_FILES ) ) {
				$file_name = $_FILES['async-upload']['name'];
				$lecture_id = $_POST['lecture_id'];
				require_once(ABSPATH . 'wp-admin/includes/admin.php');
				$id = media_handle_upload('async-upload', $lecture_id);
				unset($_FILES);
				if ( is_wp_error($id) ) {
					$errors['upload_error'] = $id;
					$id = false;
				}
				if ($errors) {
					echo "<p>There was an error uploading your file.</p>";
				} else {
					
					$woodb->createMaterial($lecture_id, $file_name, $id,  $_POST['material_type']);
					if (isset( $_REQUEST['goto'])) {
						$_SERVER['REQUEST_METHOD'] = 'GET';
						load_page($_REQUEST['goto']);
					}
				}
			}
			break;
	}
}

add_action("admin_head", "addAdminToHeader", 1);
add_action('admin_menu', 'create_admin_menu');
?>